package br.ufpb.sistcomp.persistence;

import java.util.ArrayList;
import java.util.List;

import br.ufpb.sistcomp.database.InformationDatabase;
import br.ufpb.sistcomp.entity.Discipline;
import br.ufpb.sistcomp.entity.Semester;
import br.ufpb.sistcomp.entity.Teacher;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * DisciplanePersisten entity class
 * 
 * @author Diego Sousa, www.diegosousa.com
 * @author Fagner Silva Martins, fagner.silva@dce.ufpb.br
 * @version 1.0
 * @since 02/02/2012
 * 
 *        Copyright (C) 2012 SistComp Empresa Jr.
 */

public class DisciplinePersistence {

	private static DisciplinePersistence instance;
	private static SQLiteDatabase sqliteDatabase;
	private ContentValues values;
	private Context context;
	private TeacherPersistence teacherPersistence;
	private SemesterPersistence semesterPersistence;

	private DisciplinePersistence() {
		this.values = new ContentValues();
		this.teacherPersistence = TeacherPersistence.getInstance(context);
		this.semesterPersistence = SemesterPersistence.getInstance(context);
	}

	public static synchronized DisciplinePersistence getInstance(Context context) {

		if (instance == null) {
			instance = new DisciplinePersistence();
			sqliteDatabase = context.openOrCreateDatabase(
					InformationDatabase.NAME_DATABASE, Context.MODE_PRIVATE,
					null);
		}
		return instance;
	}

	public void insertDiscipline(Discipline discipline, Teacher teacher,
			Semester semester) {

		Teacher teacherAux = teacherPersistence
				.locateTeacher(teacher.getName());
		Semester semesterAux = semesterPersistence.locateSemester(semester
				.getName());

		if (teacherAux != null) {
			values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_TEACHER,
					teacherAux.getId());
		} else {
			// exception
		}

		if (semesterAux != null) {
			values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_SEMESTER,
					semesterAux.getId());
		} else {
			// exception
		}

		values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_NAME,
				discipline.getName());
		values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_MEDIA,
				discipline.getMedia());
		values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_HOURS,
				discipline.getHours());

		sqliteDatabase.insert(InformationDatabase.GETNAME_TABLE_DISCIPLINES,
				null, values);

	}

	public void removeDisciplines(String nameDiscipline) {
		
		sqliteDatabase.delete(InformationDatabase.GETNAME_TABLE_DISCIPLINES,
				InformationDatabase.ATRIBUTE_DISCIPLINES_NAME + "="
						+ "'"+nameDiscipline+"'", null);
	}

	public void updateDisciplines(Discipline discipline) {

		String where = InformationDatabase.ATRIBUTE_DISCIPLINES_ID_DISCIPLINES
				+ "=" + discipline.getId();

		values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_DISCIPLINES,
				discipline.getId());
		values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_TEACHER,
				discipline.getIdteacher());
		values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_SEMESTER,
				discipline.getIdSemester());
		values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_NAME,
				discipline.getName());
		values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_MEDIA,
				discipline.getMedia());
		values.put(InformationDatabase.ATRIBUTE_DISCIPLINES_HOURS,
				discipline.getHours());

		sqliteDatabase.update(InformationDatabase.GETNAME_TABLE_DISCIPLINES,
				values, where, null);
	}

	public Discipline locateDisciplines(String nameDiscipline) {

		Discipline discipline = null;

		Cursor cursorDiscipline = sqliteDatabase
				.query(InformationDatabase.GETNAME_TABLE_DISCIPLINES,
						new String[] {
								InformationDatabase.ATRIBUTE_DISCIPLINES_ID_DISCIPLINES,
								InformationDatabase.ATRIBUTE_DISCIPLINES_ID_TEACHER,
								InformationDatabase.ATRIBUTE_DISCIPLINES_ID_SEMESTER,
								InformationDatabase.ATRIBUTE_DISCIPLINES_NAME,
								InformationDatabase.ATRIBUTE_DISCIPLINES_MEDIA,
								InformationDatabase.ATRIBUTE_DISCIPLINES_HOURS },
						null, null, null, null, null);

		// course is before the last line, the cursor moves to next line.
		if (cursorDiscipline.getPosition() == -1) {
			cursorDiscipline.moveToFirst();
		}

		if (cursorDiscipline != null) {
			while (!cursorDiscipline.isAfterLast()) {// Repeat until the last
				if (nameDiscipline
						.equals(cursorDiscipline
								.getString(
										cursorDiscipline
												.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_NAME))
								.toString())) {
					discipline = new Discipline();

					discipline
							.setId(cursorDiscipline.getInt(cursorDiscipline
									.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_DISCIPLINES)));
					discipline
							.setIdteacher(cursorDiscipline.getInt(cursorDiscipline
									.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_TEACHER)));
					discipline
							.setIdSemester(cursorDiscipline.getInt(cursorDiscipline
									.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_SEMESTER)));
					discipline
							.setName(cursorDiscipline.getString(cursorDiscipline
									.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_NAME)));
					discipline
							.setMedia(cursorDiscipline.getDouble(cursorDiscipline
									.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_MEDIA)));
					discipline
							.setHours(cursorDiscipline.getString(cursorDiscipline
									.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_HOURS)));
					return discipline;
				} else {
					cursorDiscipline.moveToNext();
				}
			}// end while.
		}
		return null;
	}

	public List<Discipline> listAllDisciplines() {
		
		Discipline disciplines = null;
		ArrayList<Discipline> listDiscipline = new ArrayList<Discipline>();

		Cursor cursorDiscipline = sqliteDatabase
				.query(InformationDatabase.GETNAME_TABLE_DISCIPLINES,
						new String[] {
								InformationDatabase.ATRIBUTE_DISCIPLINES_ID_DISCIPLINES,
								InformationDatabase.ATRIBUTE_DISCIPLINES_ID_TEACHER,
								InformationDatabase.ATRIBUTE_DISCIPLINES_ID_SEMESTER,
								InformationDatabase.ATRIBUTE_DISCIPLINES_NAME,
								InformationDatabase.ATRIBUTE_DISCIPLINES_MEDIA,
								InformationDatabase.ATRIBUTE_DISCIPLINES_HOURS },
						null, null, null, null, null);

		if (cursorDiscipline.getPosition() == -1) {
			cursorDiscipline.moveToFirst();
		}
		if (cursorDiscipline != null) {
			while (!cursorDiscipline.isAfterLast()) {// Repeat until the last

				disciplines = new Discipline();
				disciplines
						.setId(cursorDiscipline.getInt(cursorDiscipline
								.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_DISCIPLINES)));
				disciplines
						.setIdteacher(cursorDiscipline.getInt(cursorDiscipline
								.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_TEACHER)));
				disciplines
						.setIdSemester(cursorDiscipline.getInt(cursorDiscipline
								.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_ID_SEMESTER)));
				disciplines
						.setName(cursorDiscipline.getString(cursorDiscipline
								.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_NAME)));
				disciplines
						.setMedia(cursorDiscipline.getDouble(cursorDiscipline
								.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_MEDIA)));
				disciplines
						.setHours(cursorDiscipline.getString(cursorDiscipline
								.getColumnIndex(InformationDatabase.ATRIBUTE_DISCIPLINES_HOURS)));

				listDiscipline.add(disciplines);
				cursorDiscipline.moveToNext();
			}
		}
		return listDiscipline;
	}
}
